﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class PurchaseOrderShow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             if (Request.QueryString["SalesOrderID"] != null)

                {
                    if (mk.IsNumber(Request.QueryString["SalesOrderID"].ToString()))
                    {
                        //用一个隐藏Label保存传递过来的ID
                        this.lblSalesOrderID.Text = Request.QueryString["SalesOrderID"].ToString();
                    }
                    else
                    {
                       mk.Alert("参数错误！");
                    }
                }
                else
                {
                   mk.Alert("参数错误！");
                }
                //设置经办人
                Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                 this.lblRootDepartmentID.Text = bllDept.Where(j=>j.ClassID==(int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                var bllOrder =mk.l.Sales_SalesOrderHeader.ToList();
                       Sales_SalesOrderHeader mOrder = bllOrder.Where(h=>h.SalesOrderID==int.Parse(this.lblSalesOrderID.Text)).FirstOrDefault();
                if (int.Parse(this.lblRootDepartmentID.Text) == mOrder.RootDepartmentID)
                {
                    //绑定指定订单的订货单列表
                    this.BindPurchaseOrderHeader();
                }
                else
                {
                   mk.Alert("只能查看本店的订单！");
                }

                //显示订单概况
                //this.ShowSalesOrderHeaderInfo();
                //this.BindManufacturerID();
                //设置经办人信息
                //UART.Accounts.Bus.User currentUser = (UART.Accounts.Bus.User)(Session["UserInfo"]);
                //this.lblManagerTrueName.Text = currentUser.TrueName;
                //this.lblManagerUserName.Text = currentUser.UserName;
                //给日期输入框添加只读属性，不能在属性里直接设置，不然会取不到值
                //this.txtDateOrder.Attributes["readonly"] = "true";
                //this.txtDateOrder.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
            
        }
     private void BindPurchaseOrderHeader()
        {
            var bll = mk.l.Sales_PurchaseOrderHeader.ToList();
            //需要把最后的逗号去掉
        var  ds = bll.Where(y=>y.SalesOrderID.ToString()==this.lblSalesOrderID.Text).ToList();
            this.myDataList.DataSource = ds;
            this.myDataList.DataBind();
        }

        /// <summary>
        /// Handles the ItemDataBound event of the myDataList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.DataListItemEventArgs"/> instance containing the event data.</param>
        protected void myDataList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //绑定订货单明细列表
                GridView myGridViewPurchaseOrderDetail = (GridView)e.Item.FindControl("myGridViewPurchaseOrderDetail");
                if (myGridViewPurchaseOrderDetail != null)
                {
                    this.BindPurchaseOrderDetail(myGridViewPurchaseOrderDetail, this.myDataList.DataKeys[e.Item.ItemIndex].ToString());
                }
                //显示订货单概况信息
                Label lblOrderNumber = (Label)e.Item.FindControl("lblOrderNumber");
                Label lblManufacturerName = (Label)e.Item.FindControl("lblManufacturerName");
                Label lblManager = (Label)e.Item.FindControl("lblManager");
                Label lblDateOrder = (Label)e.Item.FindControl("lblDateOrder");
                Label lblDatePayment = (Label)e.Item.FindControl("lblDatePayment");
                Label lblSubTotal = (Label)e.Item.FindControl("lblSubTotal");
               var bllHeader = mk.l.Sales_PurchaseOrderHeader.ToList();
          Sales_PurchaseOrderHeader mHeader = bllHeader.Where(h=>h.PurchaseOrderID==int.Parse(this.myDataList.DataKeys[e.Item.ItemIndex].ToString())).FirstOrDefault();
                if(mHeader != null)
                {
                    lblOrderNumber.Text = mHeader.OrderNumber;
                    //显示厂家名称
                    var bllManufacturer = mk.l.Sales_Manufacturer.ToList();
                    Sales_Manufacturer mManufacturer = bllManufacturer.Where(h=>h.ManufacturerID==mHeader.ManufacturerID).FirstOrDefault();
                    lblManufacturerName.Text = mManufacturer.ManufacturerName;
                    //显示经办人姓名
                  var bllUser = mk.l.Accounts_Users.ToList();
                   Accounts_Users mUser = bllUser.Where(h=>h.UserName==mHeader.Manager).FirstOrDefault();
                    lblManager.Text = mUser.TrueName;
                    lblDateOrder.Text = mHeader.DateOrder.ToString();
                    //显示付款日期
                    if (mHeader.DatePayment > Convert.ToDateTime("9998-1-1"))
                    {
                        lblDatePayment.Text = "未付款";
                        lblDatePayment.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        lblDatePayment.Text = mHeader.DatePayment.ToString();
                        lblDatePayment.ForeColor = System.Drawing.Color.Green;
                    }
                    lblSubTotal.Text = mHeader.SubTotal.ToString();
                }
                
            }
        }

        /// <summary>
        /// Binds the purchase order detail.
        /// </summary>
        private void BindPurchaseOrderDetail(GridView myGridViewPurchaseOrderDetail, string PurchaseOrderID)
        {
           var bll = mk.l.Sales_MaterialandSales_PurchaseOrderDetail.ToList();
            var ds = bll.Where(y=>y.PurchaseOrderID.ToString()==PurchaseOrderID).ToList();
            myGridViewPurchaseOrderDetail.DataSource = ds;
            myGridViewPurchaseOrderDetail.DataBind();
        }

        /// <summary>
        /// Handles the RowDataBound event of the myGridViewPurchaseOrderDetail control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void myGridViewPurchaseOrderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView myGridViewNeedMaterialDetail = (GridView)(sender);
          var bll =mk.l.Sales_PurchaseOrderDetail.ToList();
               Sales_PurchaseOrderDetail m = bll.Where(h=>h.DetailID==int.Parse(myGridViewNeedMaterialDetail.DataKeys[e.Row.RowIndex].Values["DetailID"].ToString())).FirstOrDefault();
                //显示单价
                Label lblCostUnitPrice = (Label)(e.Row.FindControl("lblCostUnitPrice"));
                if (lblCostUnitPrice != null)
                {
                    if (m.CostUnitPrice == 0)
                    {
                        lblCostUnitPrice.Text = "未设置";
                    }
                    else
                    {
                        lblCostUnitPrice.Text = m.CostUnitPrice.ToString();
                    }
                }
                //显示到货状态
                Label lblStatusArrival = (Label)(e.Row.FindControl("lblStatusArrival"));
                //显示仓库
                Label lblLocation = (Label)(e.Row.FindControl("lblLocation"));

                if (lblLocation != null)
                {
                    if (m.LocationID == 0)
                    {
                        lblLocation.Text = "未入库";
                        lblLocation.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        var bllLocation = mk.l.Sales_Location.ToList();
                   Sales_Location mLocation = bllLocation.Where(h=>h.LocationID.ToString()==m.LocationID.ToString()).FirstOrDefault();
                        lblLocation.Text = mLocation.Name;
                        lblLocation.ForeColor = System.Drawing.Color.Green;
                    }
                }
                if (lblStatusArrival != null)
                {
                    if (m.StatusArrival == "0")
                    {
                        lblStatusArrival.Text = "未到货";
                        lblStatusArrival.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        lblStatusArrival.Text = "已入库";
                        lblStatusArrival.ForeColor = System.Drawing.Color.Green;
                    }
                }

                //让数字以文本形式表示
                //e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[5].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[6].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
            }
        }
    }
}